#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.util.Properties;

/**
 * Description: 无法补充
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
public class PropertiesUtil {

    private static Properties ppts = loadProperties();

    private PropertiesUtil(){
        // make constructor private
    }
    /**
     * Description:
     * <加载conf下所有的配置文件>
     * @author wupanhua
     * @date 15:42 2019/8/8
     * @return java.util.Properties
     **/
    private static Properties loadProperties() {

        Properties pps = new Properties();
        try {
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources("/conf/*.properties");
            for (Resource resource : resources) {
                String name = resource.getFilename();
                if (name.endsWith("properties")) {
                    pps.load(resource.getInputStream());
                }
            }
        } catch (Exception e) {
            log.error("加载配置文件失败:", e);
        }
        return pps;
    }

    /**
     * Description:
     * <根据key从资源文件中获取值>
     * @author wupanhua
     * @date 15:43 2019/8/8
     * @param key 1
     * @return java.lang.String
     **/
    public static String getString(String key) {
        return ppts.getProperty(key);
    }

    /**
     * Description:
     * <根据key从资源文件中获取值>
     * @author wupanhua
     * @date 15:43 2019/8/8
     * @param key 1
     * @param defaultValue 2
     * @return java.lang.String
     **/
    public static String getString(String key, String defaultValue) {
        return (String) ppts.getOrDefault(key, defaultValue);
    }

    /**
     * Description:
     * <根据key从资源文件中获取值>
     * @author wupanhua
     * @date 15:43 2019/8/8
     * @param key 1
     * @param defaultValue 2
     * @return int
     **/
    public static int getInt(String key, int defaultValue) {
        return Integer.parseInt(String.valueOf(ppts.getOrDefault(key, defaultValue)));
    }
}
